<template>
    <el-form v-if="!type" ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-descriptions :column="1" border >
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">用途　　</div>
                </template>
                <div style="width: 600px;"><el-input class="no-border-input" v-model="ruleForm.purpose" type="textarea" :autosize="{ minRows: 4 }"  /></div>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">项目名称</div>
                </template>
                <component :is="projectSelect" :data=ruleForm.project_id @onProjectSelect="onProjectSelect" />
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">资料内容</div>
                </template>
                <el-input class="no-border-input" v-model="ruleForm.content" type="content"  />
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">资料文件</div>
                </template>
                <el-button type="primary" size="small" @click="onUploadFile('files')"> 上传 </el-button>
                <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">申请人　</div>
                </template>
                <el-text>{{ ruleForm.id ? ruleForm.user.name : user.name }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">申请日期</div>
                </template>
                <el-date-picker style="width: 100%" v-model="ruleForm.end_date" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期" />
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">经办人　</div>
                </template>
                <component :is="userAllSelect" :data=ruleForm.handled_id @onUserSelect="onUserSelect" />
            </el-descriptions-item>
        </el-descriptions>
        <el-divider />
        <div class="text-center">
            <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
        </div>
    </el-form>
    
    <el-descriptions v-else :column="1" border >
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">用途</div>
            </template>
            <el-text>{{ ruleForm.purpose }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">项目名称</div>
            </template>
            <el-text>{{ ruleForm.project.title }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">资料内容</div>
            </template>
            <el-text>{{ ruleForm.content }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">资料文件</div>
            </template>
            <el-button :type="ruleForm.files ? 'primary' : 'info'" size="small" @click="onShowFile(ruleForm.files)"> 查看 </el-button>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">申请人</div>
            </template>
            <el-text>{{ ruleForm.user.name }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">申请日期</div>
            </template>
            <el-text>{{ ruleForm.end_date }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item>
            <template #label>
                <div class="cell-item">经办人</div>
            </template>
            <el-text>{{ ruleForm.handled.name }}</el-text>
        </el-descriptions-item>
    </el-descriptions>

    <el-dialog v-model="showUpload" title="文件">
        <component :is="uploadFile" :data="uploadData" @saveUpload="saveUpload" />
    </el-dialog>

    <el-dialog v-model="isShowFile" title="文件" width="80%" @close="onCloseFile">
        <component :is="showFile" :data="files"/>
    </el-dialog>

</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import http from '../util/request';
import uploadFile from '@/components/uploadFile.vue'
import showFile from '@/components/showFile.vue'
import projectSelect from "@/components/projectSelect.vue";
import userAllSelect from "@/components/userAllSelect.vue";

let user:any = sessionStorage.getItem('user')
user = JSON.parse(user)
const type = ref<boolean>(false)
const loading = ref<boolean>(false)

const files = ref('')
const isShowFile = ref<boolean>(false)
const showUpload = ref<boolean>(false)
const uploadData = ref<any>({id:0,file:'',ratio:''})

interface RuleForm {
    id: number
    project_id:number
    handled_id:number
    user_id:number
    purpose:string
    end_date:string
    content:string
    files:string
    user:any
    project:any
    handled:any
    
}

const formSize = ref<ComponentSize>('large')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    project_id:0,
    handled_id:0,
    user_id:0,
    purpose:'/',
    end_date:'',
    content:'',
    files:'',
    user:{},
    project:{},
    handled:{},
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return
    }
    http.get('/stamp/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
        
    })
}

const onShowFile = (file:string) => {
    files.value = file
    isShowFile.value = true
}

const onCloseFile = () => {
    files.value = ''
}

const onUploadFile = (type:string) => {
    uploadData.value.type = type
    if(type == 'files'){
        uploadData.value.file = ruleForm.value.files
    }
    showUpload.value = true
}

const saveUpload = (data:string) => {
    if(uploadData.value.type == 'files'){
        ruleForm.value.files = data
    }
    showUpload.value = false
}

const props = defineProps<{
    data:any
    type:boolean
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    type.value = props.type
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    purpose: [
        { required: true, message: '请输入用途', trigger: 'blur' },
    ],
    end_date: [
        { required: true, message: '请输入有效期', trigger: 'blur' },
    ],
    
})

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/stamp',ruleForm.value)
            }else{
                request = http.put('/stamp',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])

const onProjectSelect = (data:number) => {
    ruleForm.value.project_id = data
}

const onUserSelect = (data:number) => {
    ruleForm.value.handled_id = data
}

</script>
<style scoped>
.el-tab-pane{height: calc(100vh - 25vh);overflow-y: scroll;}
::v-deep .el-form-item .el-form-item__label {
  font-size: 16px; /* 或者你想要的任何大小 */
}
::v-deep .el-tabs__item{font-size: 18px !important;}

/* 全局移除 el-input 边框 */
.no-border-input {
/* 覆盖 Element Plus 的 CSS 变量 */
	--el-input-border-color: transparent;
	--el-input-focus-border-color: transparent;
	--el-input-hover-border-color: transparent;
}

/* 配合移除阴影 */
:deep(.no-border-input .el-input__inner) {
	box-shadow: none;
}
</style>
